package mo.org.cm309.shoppingcart.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import mo.org.cm309.shoppingcart.service.ProductService;

public class Cart implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HashMap<Long, Product> products;
	
	/**
	 * 
	 */
	public Cart() {
		products = new HashMap<Long, Product>();
	}

	
	public HashMap<Long, Product> getProducts() {
		return products;
	}

	public void setProducts(HashMap<Long, Product> products) {
		this.products = products;
	}

	public void addProductToCart(long pid, int qty){
		Product product = products.get(new Long(pid));
		if (product != null){
			product.setQuantity(product.getQuantity() + qty);
		}else{
			ProductService productService = new ProductService();
			product = productService.getProductById(pid);
			product.setQuantity(qty);
			products.put(new Long(pid), product);
		}
	}
	
	public List<Product> getProductList(){
		return new ArrayList<Product>(products.values());
	}
	
	public void delete(long productId){
		products.remove(productId);
	}
}
